#include "PopupView.h"
#include "PopupItem.h"
#include "PopupItemModel.h"
#include "PopupListView.h"

#include <QPushButton>
#include <QLineEdit>
#include <QEvent>
#include <QPainter>
#include <QHBoxLayout>
#include <QDateTime>
#include <QPainterPath>
#include <QScrollBar>
#include <QDebug>

QPixmap drawUserImage(const QString &text)
{
    QString name = (text);
    if (name.length() > 2) {
        name = name.mid(name.length() - 2);
    }

    QPixmap img(70,70);
    img = QPixmap(35*2,35*2);
    img.fill(Qt::transparent);

    QPainter p(&img);
    p.setRenderHints(QPainter::Antialiasing |QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

    qsrand(QDateTime::currentMSecsSinceEpoch());
    const QColor brushs[5] = {Qt::yellow,Qt::darkRed,Qt::darkGreen,Qt::darkBlue,Qt::darkMagenta};
    //    p.setBrush(QColor(63,132,248));

    QColor color = brushs[rand()%5];
    p.setBrush(color);
    p.setPen(color);

    QPainterPath path;
    path.addRoundedRect(img.rect(),10,10);
    p.setClipPath(path);
    p.drawRect(img.rect());

    QFont font;
    font.setPixelSize(29);
    p.setFont(font);
    QFontMetrics metri(font);
    int width = metri.horizontalAdvance(name);
    int height = metri.height();
    QRect textRect((img.width() - width)/2,(img.height() - height)/2 - 2,width,height);
    p.setPen(Qt::white);
    p.drawText(textRect,name);
    p.end();

    //    img.save("D:/adf1.png","PNG");
    return img;
}

PopupView::PopupView(QWidget *parent)
    : QWidget(parent)
{
    //    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    //    setWidgetShadow(this, 0, 2, QColor(0, 0, 0, 255 * 0.2), 20);
    m_delegate = new PopupItemDelegate(this);
    m_data = new PopupItemModel;

    this->setObjectName("UploadFileListWidget");

    m_headerView = new QWidget();
    m_headerView->resize(420, 30);
    m_headerView->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    qDebug() << m_headerView->height() << m_headerView->sizeHint() << m_headerView->minimumSizeHint();
    QHBoxLayout *headerLayout = new QHBoxLayout(m_headerView);


    QPushButton *button = new QPushButton(m_headerView);
    button->setText("Add");


    QLineEdit *name = new QLineEdit;
    QLineEdit *content = new QLineEdit;

    headerLayout->setContentsMargins(20, 14, 20, 14);
    headerLayout->setSpacing(10);


    QPushButton *select = new QPushButton("test",m_headerView);
    connect(select,&QPushButton::clicked,[this](){

    });

    QLineEdit *edit = new QLineEdit;


    QPushButton *remove = new QPushButton("remove",m_headerView);
    connect(remove, &QPushButton::clicked, [this,edit](){
        qDebug() << edit->text();
        if (edit->text().toInt() == 0)
            m_data->removeRow(1);
        else
            m_data->removeRow(edit->text().toInt());

    });

    headerLayout->addWidget(select);
    QLineEdit *countEdit = new QLineEdit;
    headerLayout->addWidget(countEdit);
    headerLayout->addWidget(button);
    headerLayout->addWidget(name);
    headerLayout->addWidget(content);
    headerLayout->addWidget(remove);
    headerLayout->addWidget(edit);

    connect(button,&QPushButton::clicked,[this,name,content,countEdit](){
        static int id = 0;
        int count = countEdit->text().toInt();
        if (count == 0)
            count = 1;

        while (count--) {
            MessageData data;
            data.avatar = drawUserImage(name->text());
            data.content = content->text();
            data.gifPath = "D:/work/QDemo/chatview/gif/" + QString::number((id % 20) + 1) + ".gif";
            data.id = QString::number(++id);
            data.name = name->text() + data.id;
            data.count = id;
            m_data->push(data);
        }
        listView->scrollToBottom();

//        resize(420,this->height() + 50);
    });

    m_headerView->setLayout(headerLayout);

    listView = new PopupListView();
    listView->setMinimumSize(0,0);
    listView->setItemDelegate(m_delegate);
    listView->setFrameStyle(QFrame::NoFrame);
    listView->setFocusPolicy(Qt::NoFocus);
    listView->setMouseTracking(true);
    listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    listView->setModel(m_data);

    listView->setSpacing(5);

    listView->setSelectionRectVisible(true);
    listView->show();

    connect(listView, &PopupListView::viewChange, [this](int begin, int end){
        qDebug() << begin << end;
        m_data->setGifRange(begin, end);
    });

    this->show();

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    mainLayout->addWidget(m_headerView);
    mainLayout->addWidget(listView);
    setLayout(mainLayout);

    connect(m_delegate,&PopupItemDelegate::clickItem,[this](QModelIndex index){
        //        m_data->remove(index.row());
        //        resize(420,this->height() - 50);
    });

    resize(420, 150);
}

bool PopupView::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {

    }

    return QObject::eventFilter(watched, event);
}

void PopupView::paintEvent(QPaintEvent *event)
{
    QStyleOption o;
    o.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
}

PopupView::~PopupView()
{
}
